﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.FMCG.Buz.Interfaces;
using Microhard.Core.DBUtility;
using System.Data.Common;
using Microhard.Core;
using Microhard.FMCG.Buz.DataAccess;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace Microhard.FMCG.Buz.Impls
{
    public class OrderDetailImpl : IOrderDetail
    {
        public Core.DBUtility.ExecutedResult Add(Models.OrderDetailModel model)
        {
            ExecutedResult result = new ExecutedResult();
            string addSql = "insert into orderdetail(OrderID,OrderCode,SKU,PrdName,OrderQty,ZPlanQty,StoreNo,Price,Amount,CreateTime) " +
            "values(@OrderID,@OrderCode,@SKU,@PrdName,@OrderQty,@ZPlanQty,@StoreNo,@Price,@Amount,@CreateTime)";
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@OrderID", model.OrderID));
            parms.Add(new SqlParameter("@OrderCode", model.OrderCode));
            parms.Add(new SqlParameter("@SKU", model.SKU));
            parms.Add(new SqlParameter("@PrdName", model.PrdName));
            parms.Add(new SqlParameter("@OrderQty", model.OrderQty));
            parms.Add(new SqlParameter("@ZPlanQty", string.Empty));
            parms.Add(new SqlParameter("@StoreNo", model.StoreNo));
            parms.Add(new SqlParameter("@Price", 0));
            parms.Add(new SqlParameter("@Amount", 0));
            parms.Add(new SqlParameter("@CreateTime", DateTime.Now));
            try
            {
                var rtn = SqlHelper.ExecuteNonQuery(DBHelper.GetCNN(),
                    System.Data.CommandType.Text,
                    addSql,
                    parms.ToArray());
            }
            catch (Exception ex)
            {
                Log.Error("OrderDetailImpl.Add", ex);
                result.ErrorMsg.Add("Add", "添加订单明细失败");
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Update(Models.OrderDetailModel model)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ExecutedResult Delete(Models.OrderDetailModel model)
        {
            throw new NotImplementedException();
        }

        public Models.OrderDetailModel GetModelByID(Guid id)
        {
            throw new NotImplementedException();
        }

        public Models.OrderDetailModel GetModelByModel(Models.OrderDetailModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.OrderDetailModel> Query(Models.OrderDetailModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            throw new NotImplementedException();
        }

        public List<Models.OrderDetailModel> Query(Models.OrderDetailModel Model)
        {
            List<Models.OrderDetailModel> result = new List<Models.OrderDetailModel>();
            string addSql = "select * from orderdetail where OrderID = @OrderID";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@OrderID", Model.OrderID));

            try
            {
                var ds = SqlHelper.ExecuteDataset(DBHelper.GetCNN(),
                    System.Data.CommandType.Text,
                    addSql,
                    parms.ToArray());
                if (ds != null)
                {
                    for (int n = 0; n < ds.Tables[0].Rows.Count; n++)
                    {
                        result.Add(new Models.OrderDetailModel
                        {
                            OrderCode = ds.Tables[0].Rows[n]["OrderCode"].ToString(),
                            OrderID = long.Parse(ds.Tables[0].Rows[n]["OrderID"].ToString()),
                            OrderQty = int.Parse(ds.Tables[0].Rows[n]["OrderQty"].ToString()),
                            PrdName = ds.Tables[0].Rows[n]["PrdName"].ToString(),
                            SKU = ds.Tables[0].Rows[n]["SKU"].ToString(),
                            StoreNo = ds.Tables[0].Rows[n]["StoreNo"].ToString()
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error("OrderImpl.Query", ex);
            }
            return result;
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
